package com.it.miniodemo.controller;

import com.it.miniodemo.mapper.SysUploadTaskMapper;
import com.it.miniodemo.minio.InitTaskParam;
import com.it.miniodemo.minio.Result;
import com.it.miniodemo.model.SysUploadTask;
import com.it.miniodemo.model.dto.UploadDto;
import com.it.miniodemo.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author
 * @date 2023/2/8 21:05
 **/
//@Res
@RestController
@RequestMapping("minio")
public class MinioController {

    @Autowired
    private SysUploadTaskMapper sysUploadTaskMapper;
    @Autowired
    private MinioService minioService;

    @RequestMapping("test")
    public String t1() {
        final List<SysUploadTask> sysUploadTasks = sysUploadTaskMapper.selectList(null);
        return "11";
    }

    /***
     * @description 初始化一个上传任务  2
     *
     * @return java.lang.String
     * @date 2023/2/8 22:15
     *
     */
    @PostMapping("")
    public Result initTask(@RequestBody InitTaskParam initTaskParam) {
        final LocalDateTime currentTime = LocalDateTime.now();

        return minioService.initTask(initTaskParam);

    }

    /***
     * @description 获取预上传地址
     * @param uploadDto
     * @return com.it.miniodemo.minio.Result
     * @date 2023/2/9 0:52
     *
     */
    @PostMapping("getUploadPath")
    public Result preSignUploadUrl(@RequestBody UploadDto uploadDto) {
//        uploadId从数据库中获取
        return minioService.preSignUploadUrl(uploadDto);
    }


    /***
     * @description 获取文件上传的进度，还有多少分块没上传
     * @param fileMd5
     * @return com.it.miniodemo.minio.Result
     * @date 2023/2/9 0:53
     *
     */
    @GetMapping("taskInfo/{fileMd5}")
    public Result taskInfo(@PathVariable("fileMd5") String fileMd5) {

        return minioService.taskInfo(fileMd5);
    }

    /**
     * 合并分片
     *
     * @param identifier
     * @return
     */
    @PostMapping("/merge/{fileMd5}")
    public Result merge(@PathVariable("fileMd5") String fileMd5) {
        minioService.merge(fileMd5);
        return Result.ok();
    }
}
